package cn.easybuy.service.order;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;

import cn.easybuy.dao.order.*;
import cn.easybuy.dao.product.ProductDao;
import cn.easybuy.dao.product.ProductDaoImpl;
import cn.easybuy.dao.product.ProductMapper;
import cn.easybuy.utils.*;
import cn.easybuy.entity.Order;
import cn.easybuy.entity.OrderDetail;
import cn.easybuy.entity.User;
import org.apache.ibatis.session.SqlSession;

public class OrderServiceImpl implements OrderService {

    /**
     * 结算购物车物品包含以下步骤：
     * 1.生成订单
     * 2.生成订单明细
     * 3.更新商品表，减库存
     * 注意加入事物的控制
     */

    @Override
    public Order payShoppingCart(ShoppingCart cart, User user, String address) {
//        // TODO Auto-generated method stub
//        Connection connection = null;
//        Order order = new Order();
//        try {
//            connection = DataSourceUtil.openConnection();
//            connection.setAutoCommit(false);
//            ProductDao productDao = new ProductDaoImpl(connection);
//            OrderDao orderDao = new OrderDaoImpl(connection);
//            OrderDetailDao orderDetailDao = new OrderDetailDaoImpl(connection);
//            //增加订单
//            order.setUserId(user.getId());
//            order.setLoginName(user.getLoginName());
//            order.setUserAddress(address);
//            order.setCreateTime(new Date());
//            order.setCost(cart.getTotalCost());
//            order.setSerialNumber(StringUtils.randomUUID());
//            orderDao.add(order);
//            //增加订单对应的明细信息
//            for (ShoppingCartItem item : cart.getItems()) {
//                OrderDetail orderDetail = new OrderDetail();
//                orderDetail.setOrderId(order.getId());
//                orderDetail.setCost(item.getCost());
//                orderDetail.setProduct(item.getProduct());
//                orderDetail.setQuantity(item.getQuantity());
//                orderDetailDao.add(orderDetail);
//                //更新商品表的库存
//                productDao.updateStock(item.getProduct().getId(), item.getQuantity());
//                connection.commit();
//            }
//        } catch (Exception e) {
//            // TODO: handle exception
//            e.printStackTrace();
//            try {
//                connection.rollback();
//            } catch (SQLException e1) {
//                e1.printStackTrace();
//            }
//            order = null;
//        } finally {
//            try {
//                connection.setAutoCommit(true);
//                DataSourceUtil.closeConnection(connection);
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
        Order order = new Order();
        SqlSession sqlSession=null;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            //增加订单
            order.setUserId(user.getId());
            order.setLoginName(user.getLoginName());
            order.setUserAddress(address);
            order.setCreateTime(new Date());
            order.setCost(cart.getTotalCost());
            order.setSerialNumber(StringUtils.randomUUID());
            sqlSession.getMapper(OrderMapper.class).add(order);
            //增加订单对应的明细信息
            for (ShoppingCartItem item : cart.getItems()) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOrderId(order.getId());
                orderDetail.setCost(item.getCost());
                orderDetail.setProduct(item.getProduct());
                orderDetail.setProductId(order.getId());
                orderDetail.setQuantity(item.getQuantity());
                sqlSession.getMapper(OrderDetailMapper.class).add(orderDetail);
                //更新商品表的库存
                sqlSession.getMapper(ProductMapper.class).updateStock(item.getProduct().getId(),
                        item.getQuantity());
            }
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            order=null;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return order;
    }

    @Override
    public List<Order> getOrderList(Integer userId, Integer currentPageNo, Integer pageSize) {
//        Connection connection = null;
//        List<Order> orderList = new ArrayList<Order>();
//        try {
//            connection = DataSourceUtil.openConnection();
//            OrderDao orderDao = new OrderDaoImpl(connection);
//            OrderDetailDao orderDetailDao=new OrderDetailDaoImpl(connection);
//            orderList = orderDao.getOrderList(userId, currentPageNo, pageSize);
//            for(Order order:orderList){
//            	order.setOrderDetailList(orderDetailDao.getOrderDetailList(order.getId()));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            DataSourceUtil.closeConnection(connection);
//            return orderList;
//        }
        SqlSession sqlSession=null;
        List<Order> orderList = new ArrayList<Order>();
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            orderList=sqlSession.getMapper(OrderMapper.class).getOrderList(userId,currentPageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            orderList=null;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return orderList;

    }

    @Override
    public int count(Integer userId) {
//        Connection connection = null;
////        Integer count=0;
////        try {
////            connection = DataSourceUtil.openConnection();
////            OrderDao orderDao = new OrderDaoImpl(connection);
////            count=orderDao.count(userId);
////        } catch (Exception e) {
////            e.printStackTrace();
////        } finally {
////            DataSourceUtil.closeConnection(connection);
////            return count;
////        }
        SqlSession sqlSession=null;
        int count=0;
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            count=sqlSession.getMapper(OrderMapper.class).getOrderCountByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            count=0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 调用dao接口：OrderDetailMapper的方法实现
     */
    @Override
    public List<OrderDetail> getOrderDetailList(Integer orderId) {
//        Connection connection = null;
//        List<OrderDetail> orderDetailList = new ArrayList<OrderDetail>();
//        try {
//            connection = DataSourceUtil.openConnection();
//            OrderDetailDao orderDetailDao = new OrderDetailDaoImpl(connection);
//            orderDetailList = orderDetailDao.getOrderDetailList(orderId);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            DataSourceUtil.closeConnection(connection);
//            return orderDetailList;
//        }
        SqlSession sqlSession=null;
        List<OrderDetail> orderDetailList=new ArrayList<>();
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            orderDetailList=sqlSession.getMapper(OrderDetailMapper.class).getOrderDetailList(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            orderDetailList=null;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return orderDetailList;
    }
}
